<template>
  <el-dialog
    :title="!dataForm.orgId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="介绍" prop="orgIntroduce">
      <el-input v-model="dataForm.orgIntroduce" placeholder="介绍"></el-input>
    </el-form-item>
    <el-form-item label="图片" prop="photo">
      <single-upload ref="singleUpload" v-model="dataForm.photo"></single-upload>
    </el-form-item>
    <el-form-item label="视频" prop="videoAddress">
      <video-upload ref="videoUpload" v-model="dataForm.videoAddress"></video-upload>
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import singleUpload from "@/components/upload/singleUpload.vue";
import videoUpload from "@/components/upload/videoUpload.vue";
  export default {
  components : {singleUpload,videoUpload},
    data () {
      return {
        visible: false,
        dataForm: {
          orgId: 0,
          userId: '',
          name: '',
          orgIntroduce: '',
          photo: '',
          videoAddress: '',
          phone: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          orgIntroduce: [
            { required: true, message: '介绍不能为空', trigger: 'blur' }
          ],
          photo: [
            { required: true, message: '图片不能为空', trigger: 'blur' }
          ],
          videoAddress: [
            { required: true, message: '视频地址不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orgId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orgId) {
            this.$http({
              url: this.$http.adornUrl(`/heartSelf/shOrg/orgInfo/${this.dataForm.orgId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.shOrg.userId
                this.dataForm.name = data.shOrg.name
                this.dataForm.orgIntroduce = data.shOrg.orgIntroduce
                this.dataForm.photo = data.shOrg.photo
                this.dataForm.videoAddress = data.shOrg.videoAddress
                this.dataForm.phone = data.shOrg.phone
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/heartSelf/shOrg/${!this.dataForm.orgId ? 'saveOrg' : 'updateOrg'}`),
              method: 'put',
              data: this.$http.adornData({
                'orgId': this.dataForm.orgId || undefined,
                'userId': this.dataForm.userId,
                'name': this.dataForm.name,
                'orgIntroduce': this.dataForm.orgIntroduce,
                'photo': this.dataForm.photo,
                'videoAddress': this.dataForm.videoAddress,
                'phone': this.dataForm.phone
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
